﻿using Microsoft.AspNetCore.Mvc;
using RobosimAPI.Class;
namespace RobosimAPI.Controllers
{
	[Route("back/Api2/Client/loginRobosim2")]
	public class LoginController : ControllerBase
	{		
		[HttpPost]
		public IActionResult Login([FromForm] LoginModel loginData)
		{
			if (string.IsNullOrEmpty(loginData.mobile)) 
			{ 
				return Ok(new ErrorResponse() { status=-1, errmsg= "_MISS_MOBILE_" });
			}
			if (string.IsNullOrEmpty(loginData.password))
			{
				return Ok(new ErrorResponse() { status=-1, errmsg= "_MISS_PASSWORD_" });
			}
			if (string.IsNullOrEmpty(loginData.version))
			{
				return Ok(new ErrorResponse() { status=-1, errmsg= "_MISS_VERSION_" });
			}
			Account account = new Account(loginData.mobile);
			if (!account.IsValid())
			{
				return Ok(new ErrorResponse() { status=-1, errmsg= "_ACCOUNT_OR_PASSWORD_INCORRECT_" });
			}
			if (account.GetPasswordMD5()!=loginData.password)
			{
				return Ok(new ErrorResponse() { status=-1, errmsg= "_ACCOUNT_OR_PASSWORD_INCORRECT_" });
			}
			if (account.IsBaned())
			{
				return Ok(new ErrorResponse() { status=-1, errmsg= "_ILLEGAL_ACCOUNT_" });
			}
			account.token = Tools.CreatToken(account.user_id.ToString());
			account.UpdateToken();
            return Ok(new SuccessResponse()
			{
				status=1,
				data= new LoginResponse()
				{
					id = account.user_id,
					jmd_id = account.user_id,
					mobile = account.account_name,
					token = account.token,
					server_time = Tools.GetTimestamp(),
					vip = account.vip,
					version = loginData.version,
					valid_start_time = "",
					valid_end_time = "",
					pro_map_for_free = null,
					class_status = 0
				}
			});
		}
		public class LoginModel
		{
			public string? mobile { get; set; }
			public string? password { get; set; }
			public string? version { get; set; }
		}
		public class LoginResponse
		{
			public int id { get; set; }
			public int jmd_id { get; set; }
			public string mobile { get; set; }
			public string token { get; set; }
			public long server_time { get; set; }
			public int vip { get; set; }
			public string version { get; set; }
			public string valid_start_time { get; set; }
			public string valid_end_time { get; set; }
			public int[] pro_map_for_free { get; set; }
			public int class_status { get; set; }
		}
	}
}
